#!/usr/bin/env python

import random

def leftUpTriangle(num):
    for i in range(0, num):
        print("* " * (num - i))

def leftDownTriangle(num):
    for i in range(1, num + 1):
        print("* " * i )


def rightUpTriangle(num):
    for i in range(0, num):
        print("  " * i + "* " * (num -  i))

def rightDownTriangle(num):
    for i in range(1, num + 1):
        print("  " * (num - i) + "* " * i)

def trapezoid(num1, num2):
    for i in range(0, num2):
        print("  " * (num2 - 1 - i) + "* " * (num1 + 2 * i))

def diamond(num):
    for i in range(1, num + 1):
        print("  " * (num - i) + "* " * (2 * i - 1))
    for i in range(1, num):
        print("  " * i + "* " *(2 *  (num - i) - 1))

def guessNumGame():
    while True:
        num = random.randint(1, 100)
        while True:
            guessNum = int(input("请输入你猜的数字："))
            if guessNum > num:
                print("你猜的数大了")
            elif guessNum < num:
                print("你猜的数小了")
            else:
                print("恭喜你猜对了！")
                break
        f=input("是否继续下一轮游戏（y/n）?")
        if f == "y":
            pass
        else:
            break

def finishedNum(num):
    sum = 0
    for i in range(1, num // 2 + 1):
        if num % i == 0:
            sum += i
    return sum == num

def countFinishedNum(num):
    amount = 0
    for i in range(1, num + 1):
        if finishedNum(i):
            amount += 1
            print(i)
    print("一共含有" + str(amount) + "个完数")

def narcissusNum(number):
    if number < 100:
        return False
    sum = 0;
    num = number
    i = len(str(num))
    while num > 0:
        sum += (num % 10)**i
        num //= 10
    return sum==number

def countNarcissusNum(num):
    amount = 0
    for i in range(1, num + 1):
        if narcissusNum(i):
            print(i)
            amount += 1
    print("一共含有" + str(amount) + "个水仙花数")

def rectangle(num1, num2):
    for i in range(0, num2):
        print("* " * num1)

def triangle():
    while True:
        print("1). 左上直角三角形")
        print("2). 左下直角三角形")
        print("3). 右上直角三角形")
        print("4). 右下直角三角形")
        print("5). 返回上一级")
        print("6). 退出")
        sel = int(input("请输入[1 - 6]:"))
        if sel ==1:
            leftUpTriangle(int(input("请输入边长：")))
        elif sel == 2:
             leftDownTriangle(int(input("请输入边长：")))
        elif sel == 3:
             rightUpTriangle(int(input("请输入边长：")))
        elif sel == 4:
              rightDownTriangle(int(input("请输入边长：")))
        elif sel == 5:
            return True
        elif sel == 6:
            return False

flag = True
while flag:
    print("1、三角形")
    print("2、梯形")
    print("3、矩形")
    print("4、菱形")
    print("5、猜数字小游戏")
    print("6、完数")
    print("7、水仙花数")
    print("8、退出")
    sel=int(input("请输入[1 - 8]:"))
    if sel == 1:
        flag = triangle()
    elif sel == 2:
        trapezoid(int(input("请输入上底边长：")), int(input("请输入高：")))
    elif sel == 3:
        rectangle(int(input("请输入宽：")), int(input("请输入高：")))
    elif sel == 4:
        diamond(int(input("请输入数字：")))
    elif sel == 5:
        guessNumGame()
    elif sel == 6:
        countFinishedNum(int(input("请输入数字：")))
    elif sel == 7:
        countNarcissusNum(int(input("请输入数字：")))
    elif sel == 8:
        break




